package cn.tedu._05weibo.controller;


import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibos/")
public class WeiboController {

    @Autowired
    public WeiboMapper weiboMapper;

    @PostMapping("insert")
    public  int insertWeibo(@RequestBody WeiboDTO weiboDTO, HttpSession session) {
            /**
             * 1/接收客户数据
             * 2/检验登录状态     session.getAttribute("user")
             *  2.1未登录，return 2；
             *  2.2登录状态：微博表中插入数据源 【INSERT接口】，return 1
             */
            UserVO userVO = (UserVO) session.getAttribute("user");
//            未登录
            if(userVO == null){
                return 2;
            }
//             登录成功，插入数据
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO,weibo);
            weibo.setUserId(userVO.getId());
            weibo.setCreated(new Date());

            weiboMapper.insertWeibo(weibo);
            return 1;
        }


    @GetMapping("selectIndx")
    public List<WeiboVO> selectIndx(){

        List<WeiboVO> weiboVO = weiboMapper.selsctIndex();

        return weiboVO;
    }

    @GetMapping("selectById")
    public WeiboDetailVO selectById(int id){

       WeiboDetailVO  weiboDetailVO = weiboMapper.selectById(id);

       return weiboDetailVO;
    }


}
